package jp.sourceforge.akjrcp.text_douga;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.text_douga.audio.AudioInput;
import jp.sourceforge.akjrcp.text_douga.audio.AudioUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AudioSystem {
	public static final int FOREEVER=-1;
	public static Log log=LogFactory.getLog(AudioSystem.class);
private List<AudioData> audioDatas=new ArrayList<AudioData>();
private double maxTime;
private String ffmpegPath;//should writer?
private int rate=48000;
private int channel=2;
private DougaPreference dougaPreference = new DougaPreference(); ;
public void addAudioData(AudioData audioData){
	audioDatas.add(audioData);
}

public AudioData[] getAudioDatas(){
	return audioDatas.toArray(new AudioData[audioDatas.size()]);
}

public void writeDatas(OutputStream outputStream) throws IOException{
	long maxBytes=(long)(rate*channel*2*maxTime);
	AudioData[] audios=getAudioDatas();
	long remain=maxBytes;
	for (int i = 0; i < audios.length; i++) {
		long writed=audios[i].writeData(dougaPreference,outputStream,remain);
		if(writed>0){
			remain-=writed;
		}
		log.info("audiowrite:writed="+writed+" remain="+remain+" maxbyte="+maxBytes);
		DougaSystem.dougaLogger.info(Messages.getString("log.finish_audio_sequence")+":"+(i+1)+" of "+audios.length);
	}
	
	outputStream.flush();//very important
	outputStream.close();
}

public String getFfmpegPath() {
	return ffmpegPath;
}

public void setFfmpegPath(String ffmpegPath) {
	this.ffmpegPath = ffmpegPath;
}

public double getMaxTime() {
	return maxTime;
}

public void setMaxTime(double maxTime) {
	this.maxTime = maxTime;
}

public DougaPreference getDougaPreference() {
	return dougaPreference;
}

public void setDougaPreference(DougaPreference dougaPreference) {
	this.dougaPreference = dougaPreference;
}

}
